import {
    Taxon,
    Species,
    CreateSpeciesBookParams,
    ParagraphType,
    ReportParagraphBlock,
    SpeciesInfo,
    CategoryWithChildren,
    SpeciesDataWithIndexSignatures,
    ReportGenerateResult
} from "./typings/index";
import {
    generateSpeciesStatistics,
    getUUID,
    generateWordParagraBlockByPlainText,
    isEmpty,
    getChineseNumber,
    classifyOrganismV2
} from "./utils";

type SpeciesBookTableRow = [string, string, string, string, string, string];
type ProtectionSpeciesBookTableRow = [string, string, string, string, string, string, string, string];
const speciesBookTableRowTemplate: SpeciesBookTableRow = ["", "", "", "", "", ""];
const speciesBookTableHeader = ["序号", "中文名", "拉丁名", "国家保护级别", "IUCN", "CITES 附录"];

const protectionSpeciesBookTableRowTemplate: ProtectionSpeciesBookTableRow = ["", "", "", "", "", "", "", ""];
const protectionSpeciesBookTableHeader = ["编号", "分类", "物种名称", "拉丁名", "国家保护级别", "IUCN", "CITES 附录"];

const classifyOrganismV1 = (speciesInfoRow: SpeciesInfo) => {
    let classification = "未知";

    if (speciesInfoRow.kingdom === 'Plantae' || speciesInfoRow.kingdom_c === '植物界') {
        classification = '植物';
        if (speciesInfoRow.phylum.includes('Pteridophyta') || speciesInfoRow.phylum_c.includes('蕨类植物门')) {
            classification = '蕨类植物';
        } else if (speciesInfoRow.phylum.includes('Magnoliophyta') || speciesInfoRow.phylum_c.includes('被子植物门')) {
            classification = '被子植物';
        } else if (speciesInfoRow.phylum.includes('Coniferophyta') || speciesInfoRow.phylum_c.includes('裸子植物门')) {
            classification = '裸子植物';
        } else if (speciesInfoRow.phylum.includes('Fungi') || speciesInfoRow.phylum_c.includes('真菌门')) {
            classification = '大型真菌';
        } else if (speciesInfoRow.phylum.includes('Bryophyta') || speciesInfoRow.phylum_c.includes('苔藓植物门')) {
            classification = '苔藓植物';
        } else if (speciesInfoRow.phylum.includes('Lichen') || speciesInfoRow.phylum_c.includes('地衣')) {
            classification = '地衣';
        }
    } else if (speciesInfoRow.kingdom === 'Protista' || speciesInfoRow.kingdom_c === '原生生物界' || speciesInfoRow.kingdom === 'Chromista' || speciesInfoRow.kingdom_c === '色素藻界') {
        if (speciesInfoRow.phylum.includes('Chlorophyta') || speciesInfoRow.phylum_c.includes('绿藻门')) {
            classification = '藻类植物';
        }
    } else if (speciesInfoRow.kingdom === 'Animalia' || speciesInfoRow.kingdom_c === '动物界') {
        if (speciesInfoRow.class.includes('Mammalia') || speciesInfoRow.class_c.includes('哺乳纲')) {
            classification = '兽类';
        } else if (speciesInfoRow.class.includes('Aves') || speciesInfoRow.class_c.includes('鸟纲')) {
            classification = '鸟类';
        } else if (speciesInfoRow.class.includes('Amphibia') || speciesInfoRow.class.includes('Reptilia') || speciesInfoRow.class_c.includes('两栖纲') || speciesInfoRow.class_c.includes('爬行纲')) {
            classification = '两爬类';
        } else if (speciesInfoRow.class.includes('Actinopterygii') || speciesInfoRow.class_c.includes('鱼纲')) {
            classification = '鱼类';
        }
    }
    return classification;
}


const generatePlantSpeciesBookContent = (speciesCategory: CategoryWithChildren): string[][] => {
    let tableContent: string[][] = [speciesBookTableHeader];
    let serialNumber1 = 1;
    let serialNumber2 = 1;
    let serialNumber3 = 1;
    for (const orderKey in speciesCategory) {
        const orderRecord = speciesCategory[orderKey].children;
        for (const familyKey in orderRecord) {
            let speciesBookTableRow: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
            speciesBookTableRow[0] = `(${getChineseNumber(serialNumber1)})`;
            speciesBookTableRow[1] = familyKey;
            speciesBookTableRow[2] = orderRecord[familyKey]?.latin ? orderRecord[familyKey]?.latin : '';
            tableContent.push(speciesBookTableRow);
            serialNumber1++;
            const familyRecord = orderRecord[familyKey].children;
            for (const genusKey in familyRecord) {
                let speciesBookTableRowForGenus: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                speciesBookTableRowForGenus[0] = `(${serialNumber2.toString()})`;;
                speciesBookTableRowForGenus[1] = genusKey;
                speciesBookTableRowForGenus[2] = familyRecord[genusKey]?.latin ? familyRecord[genusKey].latin : '';
                tableContent.push(speciesBookTableRowForGenus)
                serialNumber2++;
                const genusRecord = familyRecord[genusKey].children;
                for (const speciesKey in genusRecord) {
                    const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                    let speciesBookTableRowForSpecies: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                    speciesBookTableRowForSpecies[0] = serialNumber3.toString();
                    speciesBookTableRowForSpecies[1] = speciesRecord.name;
                    speciesBookTableRowForSpecies[2] = speciesRecord.latin;
                    speciesBookTableRowForSpecies[3] = speciesRecord.national;
                    speciesBookTableRowForSpecies[4] = speciesRecord.iucn;
                    speciesBookTableRowForSpecies[5] = speciesRecord.cites;
                    tableContent.push(speciesBookTableRowForSpecies)
                    serialNumber3++;
                }
            }
        }
    }
    return tableContent;
}

const generateAnimalSpeciesBookContent = (speciesCategory: CategoryWithChildren): string[][] => {
    let tableContent: string[][] = [speciesBookTableHeader];
    let serialNumber1 = 1;
    let serialNumber2 = 1;
    let serialNumber3 = 1;
    for (const orderKey in speciesCategory) {
        const orderRecord = speciesCategory[orderKey].children;
        let speciesBookTableRow: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
        speciesBookTableRow[0] = `(${getChineseNumber(serialNumber1)})`;
        speciesBookTableRow[1] = orderKey;
        speciesBookTableRow[2] = speciesCategory[orderKey]?.latin ? speciesCategory[orderKey]?.latin : '';
        tableContent.push(speciesBookTableRow);
        serialNumber1++;
        for (const familyKey in orderRecord) {
            let speciesBookTableRowForGenus: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
            speciesBookTableRowForGenus[0] = `(${serialNumber2.toString()})`;
            speciesBookTableRowForGenus[1] = familyKey;
            speciesBookTableRowForGenus[2] = orderRecord[familyKey]?.latin ? orderRecord[familyKey].latin : '';
            tableContent.push(speciesBookTableRowForGenus)
            serialNumber2++;
            const familyRecord = orderRecord[familyKey].children;
            for (const genusKey in familyRecord) {
                const genusRecord = familyRecord[genusKey].children;
                for (const speciesKey in genusRecord) {
                    const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                    let speciesBookTableRowForSpecies: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                    speciesBookTableRowForSpecies[0] = serialNumber3.toString();
                    speciesBookTableRowForSpecies[1] = speciesRecord.name;
                    speciesBookTableRowForSpecies[2] = speciesRecord.latin || "";
                    speciesBookTableRowForSpecies[3] = speciesRecord.national || "";
                    speciesBookTableRowForSpecies[4] = speciesRecord.iucn || "";
                    speciesBookTableRowForSpecies[5] = speciesRecord.cites || "";
                    tableContent.push(speciesBookTableRowForSpecies)
                    serialNumber3++;
                }
            }
        }
    }
    return tableContent;
}


const forceTransTaxon2Species = (taxon: Taxon): Species => {
    //@ts-ignore
    return taxon;
}

const generatePlantSpeciesBook = (
    speciesCategory1: CategoryWithChildren,
    speciesCategory2: CategoryWithChildren,
    speciesCategory3: CategoryWithChildren
): ReportParagraphBlock[] => {
    let plantTableParagraphBlock: ReportParagraphBlock[] = [];
    let plantTable: string[][] = [speciesBookTableHeader];
    let serialNumber0 = 1;
    let serialNumber1 = 1;
    let serialNumber2 = 1;
    let serialNumber3 = 1;
    if (speciesCategory1) {
        plantTable.push([getChineseNumber(serialNumber0), "裸子植物", "GYMNOSPERMAE", "", "", ""]);
        for (const orderKey in speciesCategory1) {
            const orderRecord = speciesCategory1[orderKey].children;
            for (const familyKey in orderRecord) {
                let speciesBookTableRow: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                speciesBookTableRow[0] = `(${getChineseNumber(serialNumber1)})`;
                speciesBookTableRow[1] = familyKey;
                speciesBookTableRow[2] = orderRecord[familyKey]?.latin ? orderRecord[familyKey]?.latin : '';
                plantTable.push(speciesBookTableRow);
                serialNumber1++;
                const familyRecord = orderRecord[familyKey].children;
                for (const genusKey in familyRecord) {
                    let speciesBookTableRowForGenus: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                    speciesBookTableRowForGenus[0] = `(${serialNumber2.toString()})`;
                    speciesBookTableRowForGenus[1] = genusKey;
                    speciesBookTableRowForGenus[2] = familyRecord[genusKey]?.latin ? familyRecord[genusKey].latin : '';
                    plantTable.push(speciesBookTableRowForGenus)
                    serialNumber2++;
                    const genusRecord = familyRecord[genusKey].children;
                    for (const speciesKey in genusRecord) {
                        const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                        let speciesBookTableRowForSpecies: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                        speciesBookTableRowForSpecies[0] = serialNumber3.toString();
                        speciesBookTableRowForSpecies[1] = speciesRecord.name;
                        speciesBookTableRowForSpecies[2] = speciesRecord.latin;
                        speciesBookTableRowForSpecies[3] = speciesRecord.national;
                        speciesBookTableRowForSpecies[4] = speciesRecord.iucn;
                        speciesBookTableRowForSpecies[5] = speciesRecord.cites;
                        plantTable.push(speciesBookTableRowForSpecies)
                        serialNumber3++;
                    }
                }
            }
        }
        serialNumber0++;
    }
    if (speciesCategory2) {
        plantTable.push([getChineseNumber(serialNumber0), "被子植物", "ANGIOSPERM", "", "", ""]);
        for (const orderKey in speciesCategory2) {
            const orderRecord = speciesCategory2[orderKey].children;
            for (const familyKey in orderRecord) {
                let speciesBookTableRow: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                speciesBookTableRow[0] = `(${getChineseNumber(serialNumber1)})`;
                speciesBookTableRow[1] = familyKey;
                speciesBookTableRow[2] = orderRecord[familyKey]?.latin ? orderRecord[familyKey]?.latin : '';
                plantTable.push(speciesBookTableRow);
                serialNumber1++;
                const familyRecord = orderRecord[familyKey].children;
                for (const genusKey in familyRecord) {
                    let speciesBookTableRowForGenus: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                    speciesBookTableRowForGenus[0] = `(${serialNumber2.toString()})`;
                    speciesBookTableRowForGenus[1] = genusKey;
                    speciesBookTableRowForGenus[2] = familyRecord[genusKey]?.latin ? familyRecord[genusKey].latin : '';
                    plantTable.push(speciesBookTableRowForGenus)
                    serialNumber2++;
                    const genusRecord = familyRecord[genusKey].children;
                    for (const speciesKey in genusRecord) {
                        const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                        let speciesBookTableRowForSpecies: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                        speciesBookTableRowForSpecies[0] = serialNumber3.toString();
                        speciesBookTableRowForSpecies[1] = speciesRecord.name;
                        speciesBookTableRowForSpecies[2] = speciesRecord.latin;
                        speciesBookTableRowForSpecies[3] = speciesRecord.national;
                        speciesBookTableRowForSpecies[4] = speciesRecord.iucn;
                        speciesBookTableRowForSpecies[5] = speciesRecord.cites;
                        plantTable.push(speciesBookTableRowForSpecies)
                        serialNumber3++;
                    }
                }
            }
        }
        serialNumber0++;
    }
    if (speciesCategory3) {
        plantTable.push([getChineseNumber(serialNumber0), "蕨类植物", "PTERIDOPHYTA", "", "", ""]);
        for (const orderKey in speciesCategory3) {
            const orderRecord = speciesCategory3[orderKey].children;
            for (const familyKey in orderRecord) {
                let speciesBookTableRow: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                speciesBookTableRow[0] = `(${getChineseNumber(serialNumber1)})`;
                speciesBookTableRow[1] = familyKey;
                speciesBookTableRow[2] = orderRecord[familyKey]?.latin ? orderRecord[familyKey]?.latin : '';
                plantTable.push(speciesBookTableRow);
                serialNumber1++;
                const familyRecord = orderRecord[familyKey].children;
                for (const genusKey in familyRecord) {
                    let speciesBookTableRowForGenus: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                    speciesBookTableRowForGenus[0] = `(${serialNumber2.toString()})`;
                    speciesBookTableRowForGenus[1] = genusKey;
                    speciesBookTableRowForGenus[2] = familyRecord[genusKey]?.latin ? familyRecord[genusKey].latin : '';
                    plantTable.push(speciesBookTableRowForGenus)
                    serialNumber2++;
                    const genusRecord = familyRecord[genusKey].children;
                    for (const speciesKey in genusRecord) {
                        const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                        let speciesBookTableRowForSpecies: SpeciesBookTableRow = [...speciesBookTableRowTemplate];
                        speciesBookTableRowForSpecies[0] = serialNumber3.toString();
                        speciesBookTableRowForSpecies[1] = speciesRecord.name;
                        speciesBookTableRowForSpecies[2] = speciesRecord.latin;
                        speciesBookTableRowForSpecies[3] = speciesRecord.national;
                        speciesBookTableRowForSpecies[4] = speciesRecord.iucn;
                        speciesBookTableRowForSpecies[5] = speciesRecord.cites;
                        plantTable.push(speciesBookTableRowForSpecies)
                        serialNumber3++;
                    }
                }
            }
        }
        serialNumber0++;
    }
    plantTableParagraphBlock.push(generateWordParagraBlockByPlainText('植物名录', 'tableName', false, false));
    plantTableParagraphBlock.push({
        blockId: getUUID(),
        type: 'Table',
        tableData: plantTable,
        tableStyle: 'speciesBook',
        colSpanInfoList: [],
        rowSpanInfoList: [],
        tableHeaderRowNumber: 1,
    })
    plantTableParagraphBlock.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级重点保护野生植物；II—国家II级重点保护野生植物；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
    return plantTableParagraphBlock;
}

const genereateProtectionSpeciesBookContentSlice = (
    speciesCategoryKey: string,
    speciesCategory: CategoryWithChildren,
    serialNumber: number
): {
    tableContentSlice: string[][],
    serialNumber: number
} => {
    let _serialNumber = serialNumber;
    let tableContent: string[][] = [];
    for (const orderKey in speciesCategory) {
        const orderRecord = speciesCategory[orderKey].children;
        for (const familyKey in orderRecord) {
            const familyRecord = orderRecord[familyKey].children;
            for (const genusKey in familyRecord) {
                const genusRecord = familyRecord[genusKey].children;
                for (const speciesKey in genusRecord) {
                    let protectionSpeciesBookTableRow: ProtectionSpeciesBookTableRow = [...protectionSpeciesBookTableRowTemplate];
                    const speciesRecord: Species = forceTransTaxon2Species(genusRecord[speciesKey]);
                    protectionSpeciesBookTableRow[0] = _serialNumber.toString();
                    protectionSpeciesBookTableRow[1] = speciesCategoryKey;
                    protectionSpeciesBookTableRow[2] = speciesRecord.name;
                    protectionSpeciesBookTableRow[3] = speciesRecord.latin;
                    protectionSpeciesBookTableRow[4] = speciesRecord.national;
                    protectionSpeciesBookTableRow[5] = speciesRecord.iucn;
                    protectionSpeciesBookTableRow[6] = speciesRecord.cites;
                    tableContent.push(protectionSpeciesBookTableRow)
                    _serialNumber++;
                }
            }
        }
    }
    return {
        tableContentSlice: tableContent,
        serialNumber: _serialNumber
    };
}

const genereateProtectionSpeciesBookContent = (
    speciesStatisticsRecord: Record<string, CategoryWithChildren>,
    type: 'plant' | 'animal'
): string[][] => {
    let serialNumberOfPlant = 1;
    let serialNumberOfAnimal = 1;
    let tableContent: string[][] = [protectionSpeciesBookTableHeader];
    let plantCategoryKeyList: string[] = ['被子植物', '裸子植物', '蕨类植物', '大型真菌'];
    let animalCategoryKeyList: string[] = ['兽类', '鸟类', '两爬', '昆虫', '鱼类'];
    const currentCategoryKeyList = type == 'animal' ? animalCategoryKeyList : plantCategoryKeyList;
    currentCategoryKeyList.forEach((categoryKey, index) => {
        if (speciesStatisticsRecord[categoryKey]) {
            const {
                tableContentSlice,
                serialNumber
            } = genereateProtectionSpeciesBookContentSlice(
                categoryKey,
                speciesStatisticsRecord[categoryKey],
                type == 'animal' ? serialNumberOfAnimal : serialNumberOfPlant
            );
            if (type == 'animal') {
                serialNumberOfAnimal = serialNumber;
            } else {
                serialNumberOfPlant = serialNumber;
            }
            if (tableContentSlice && tableContentSlice.length) {
                tableContent = tableContent.concat(tableContentSlice);
            }
        }
    })
    return tableContent;
}

const mergeStrToList = (list: string[], str: string): string[] => {
    if (!isEmpty(str) && !list.includes(str)) {
        list.push(str);
    }
    return list;
}

const createSpeciesBook = ({
    mergedRowDataList,
    isProtection = false
}: CreateSpeciesBookParams): ReportGenerateResult => {
    try {
        let reportGenerateResult: ReportGenerateResult = {
            reportParagraphList: [],
            matchFailureParagraphList: []
        }
        let mergedRowDataListWidthSpeciesInfo: SpeciesInfo[] = [];
        let withOutSpeciesInfoSpeciesNamePlant: string[] = [];
        let withOutSpeciesInfoSpeciesNameFungus: string[] = [];
        let withOutSpeciesInfoSpeciesNameBeast: string[] = [];
        let withOutSpeciesInfoSpeciesNameBird: string[] = [];
        let withOutSpeciesInfoSpeciesNameReptile: string[] = [];
        let withOutSpeciesInfoSpeciesNameInsect: string[] = [];
        let withOutSpeciesInfoSpeciesNameFish: string[] = [];
        mergedRowDataList.forEach(row => {
            const speciesType = classifyOrganismV2(row);
            if (row.canorical_me && !['其他植物', '其他脊索动物', '其他动物', '未知'].includes(speciesType)) {
                if (isProtection) {
                    if (
                        !isEmpty(row.CIETS) ||
                        !isEmpty(row.endangered) ||
                        !isEmpty(row.state_protection_grade)
                    ) {
                        mergedRowDataListWidthSpeciesInfo.push(row);
                    }
                } else {
                    mergedRowDataListWidthSpeciesInfo.push(row);
                }
            } else if (row.speciesName && row.speciesName !== '待鉴定') {
                const plantResearchTypeDict = [
                    'vegetationResearchCollect',
                    'forestResearchPlot',
                    'shrubResearchPlot',
                    'grassResearchPlot',
                    'plantResearchLine',
                    'plantResearchCollect'
                ];
                const fungusResearchTypeDict = [
                    'fungusResearchCollect',
                ];
                const beastResearchTypeDict = [
                    'beastResearchPoint',
                    'beastResearchLine',
                ];
                const birdPlantResearchTypeDict = [
                    'birdResearchLine',
                    'birdResearchPoint',
                    'birdResearcDisPoint',
                ];
                const reptilePlantResearchTypeDict = [
                    'reptileResearchLine',
                ];
                const insectPlantResearchTypeDict = [
                    'insectResearchLine',
                    'insectResearchPoint',
                ];
                const fishPlantResearchTypeDict = [
                    'fishResearchPoint',
                ];
                if (plantResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNamePlant = mergeStrToList(withOutSpeciesInfoSpeciesNamePlant, row.species_c);
                } else if (fungusResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameFungus = mergeStrToList(withOutSpeciesInfoSpeciesNameFungus, row.species_c);
                } else if (beastResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameBeast = mergeStrToList(withOutSpeciesInfoSpeciesNameBeast, row.species_c);
                } else if (birdPlantResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameBird = mergeStrToList(withOutSpeciesInfoSpeciesNameBird, row.species_c);
                } else if (reptilePlantResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameReptile = mergeStrToList(withOutSpeciesInfoSpeciesNameReptile, row.species_c);
                } else if (insectPlantResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameInsect = mergeStrToList(withOutSpeciesInfoSpeciesNameInsect, row.species_c);
                } else if (fishPlantResearchTypeDict.includes(row.researchType)) {
                    withOutSpeciesInfoSpeciesNameFish = mergeStrToList(withOutSpeciesInfoSpeciesNameFish, row.species_c);
                }
            }
        })
        const speciesStatistics: Record<string, CategoryWithChildren> = generateSpeciesStatistics(mergedRowDataListWidthSpeciesInfo);
        console.log("speciesStatistics--->", speciesStatistics)
        const { keys, values } = Object;
        const speciesStatisticsKeyList: string[] = keys(speciesStatistics);
        let reportParagraphList: ReportParagraphBlock[] = [];
        let matchFailureParagraphList: ReportParagraphBlock[] = [];
        if (
            withOutSpeciesInfoSpeciesNamePlant.length ||
            withOutSpeciesInfoSpeciesNameFungus.length ||
            withOutSpeciesInfoSpeciesNameBeast.length ||
            withOutSpeciesInfoSpeciesNameBird.length ||
            withOutSpeciesInfoSpeciesNameReptile.length ||
            withOutSpeciesInfoSpeciesNameInsect.length ||
            withOutSpeciesInfoSpeciesNameFish.length
        ) {
            matchFailureParagraphList.push(generateWordParagraBlockByPlainText('物种名录或统计运行结果反馈', 'failureTitle', false, false));
            matchFailureParagraphList.push(generateWordParagraBlockByPlainText('本系统使用的生物分类标准为APG分类系统，如果您的物种名称为其他分类系统的可能会无法匹配其分类信息，此外您记录的物种名称可能不是标准的物种中文名称而导致无法匹配分类信息，从而无法对其进行准确的统计，敬请见谅！', 'failureDesc', false, false));
            matchFailureParagraphList.push(generateWordParagraBlockByPlainText('未匹配到分类信息的物种记录名称如下：', 'failureDesc', false, false));
            let matchFailureTableData = [
                ["物种类型", "物种名称"],
                ["植物", ""],
                ["大型真菌", ""],
                ["兽类", ""],
                ["鸟类", ""],
                ["两爬", ""],
                ["昆虫", ""],
                ["鱼类", ""]
            ];
            if (withOutSpeciesInfoSpeciesNamePlant.length) {
                matchFailureTableData[1][1] = withOutSpeciesInfoSpeciesNamePlant.toString();
            }
            if (withOutSpeciesInfoSpeciesNameFungus.length) {
                matchFailureTableData[2][1] = withOutSpeciesInfoSpeciesNameFungus.toString();
            }
            if (withOutSpeciesInfoSpeciesNameBeast.length) {
                matchFailureTableData[3][1] = withOutSpeciesInfoSpeciesNameBeast.toString();
            }
            if (withOutSpeciesInfoSpeciesNameBird.length) {
                matchFailureTableData[4][1] = withOutSpeciesInfoSpeciesNameBird.toString();
            }
            if (withOutSpeciesInfoSpeciesNameReptile.length) {
                matchFailureTableData[5][1] = withOutSpeciesInfoSpeciesNameReptile.toString();
            }
            if (withOutSpeciesInfoSpeciesNameInsect.length) {
                matchFailureTableData[6][1] = withOutSpeciesInfoSpeciesNameInsect.toString();
            }
            if (withOutSpeciesInfoSpeciesNameFish.length) {
                matchFailureTableData[7][1] = withOutSpeciesInfoSpeciesNameFish.toString();
            }
            matchFailureParagraphList.push({
                blockId: getUUID(),
                tableData: matchFailureTableData,
                type: 'Table',
                tableStyle: 'failureTable',
                tableHeaderRowNumber: 1,
            })
            reportGenerateResult.matchFailureParagraphList = matchFailureParagraphList;
        }
        if (isProtection) {
            reportParagraphList.push(generateWordParagraBlockByPlainText('保护物种名录', 'heading1', false));
            reportParagraphList.push(generateWordParagraBlockByPlainText('', 'heading1', false));
            const plantTableContent = genereateProtectionSpeciesBookContent(speciesStatistics, 'plant');
            const animalTableContent = genereateProtectionSpeciesBookContent(speciesStatistics, 'animal');
            let serialNumberOfTable = 1;
            if (plantTableContent && plantTableContent.length > 1) {
                reportParagraphList.push(generateWordParagraBlockByPlainText(`表${serialNumberOfTable} 珍稀濒危植物名录`, 'tableName', serialNumberOfTable == 1 ? false : true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: plantTableContent,
                    tableStyle: 'protectionSpeciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                serialNumberOfTable++;
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级重点保护；Ⅱ—国家II级重点保护； DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (animalTableContent && animalTableContent.length > 1) {
                reportParagraphList.push(generateWordParagraBlockByPlainText(`表${serialNumberOfTable} 珍稀濒危动物名录`, 'tableName', serialNumberOfTable == 1 ? false : true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: animalTableContent,
                    tableStyle: 'protectionSpeciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级重点保护；Ⅱ—国家II级重点保护； DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
        } else {
            reportParagraphList.push(generateWordParagraBlockByPlainText('物种名录', 'heading1', false, false));
            reportParagraphList.push(generateWordParagraBlockByPlainText('', 'heading1', false));
            let plantspeciesCategory1: CategoryWithChildren = speciesStatistics["被子植物"];
            let plantspeciesCategory2: CategoryWithChildren = speciesStatistics["裸子植物"];
            let plantspeciesCategory3: CategoryWithChildren = speciesStatistics["蕨类植物"];
            if (speciesStatisticsKeyList.includes("被子植物")) {
                plantspeciesCategory1 = speciesStatistics["被子植物"];
            }
            if (speciesStatisticsKeyList.includes("裸子植物")) {
                plantspeciesCategory2 = speciesStatistics["裸子植物"];
            }
            if (speciesStatisticsKeyList.includes("蕨类植物")) {
                plantspeciesCategory3 = speciesStatistics["蕨类植物"];
            }
            if (
                plantspeciesCategory1 ||
                plantspeciesCategory2 ||
                plantspeciesCategory3
            ) {
                const plantSpeciesBookParagraphBlockList = generatePlantSpeciesBook(plantspeciesCategory1, plantspeciesCategory2, plantspeciesCategory3);
                reportParagraphList = reportParagraphList.concat(plantSpeciesBookParagraphBlockList)
            }
            if (speciesStatisticsKeyList.includes("大型真菌")) {
                const tableContent = generatePlantSpeciesBookContent(speciesStatistics["大型真菌"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('大型真菌名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (speciesStatisticsKeyList.includes("兽类")) {
                const tableContent = generateAnimalSpeciesBookContent(speciesStatistics["兽类"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('兽类名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (speciesStatisticsKeyList.includes("鸟类")) {
                const tableContent = generateAnimalSpeciesBookContent(speciesStatistics["鸟类"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('鸟类名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (speciesStatisticsKeyList.includes("两爬动物")) {
                const tableContent = generateAnimalSpeciesBookContent(speciesStatistics["两爬动物"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('两爬动物名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (speciesStatisticsKeyList.includes("昆虫")) {
                const tableContent = generateAnimalSpeciesBookContent(speciesStatistics["昆虫"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('昆虫动物名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
            if (speciesStatisticsKeyList.includes("鱼类")) {
                const tableContent = generateAnimalSpeciesBookContent(speciesStatistics["鱼类"]);
                reportParagraphList.push(generateWordParagraBlockByPlainText('鱼类名录', 'tableName', true, false));
                reportParagraphList.push({
                    blockId: getUUID(),
                    type: 'Table',
                    tableData: tableContent,
                    tableStyle: 'speciesBook',
                    colSpanInfoList: [],
                    rowSpanInfoList: [],
                    tableHeaderRowNumber: 1,
                })
                reportParagraphList.push(generateWordParagraBlockByPlainText('注：Ⅰ—国家I级保护；II—国家II级保护；DD—数据缺乏；LC—无危；NT—近危；VU—易危；EN—濒危；CR—极危；EW—野外灭绝；EX—灭绝；附录Ⅰ—列入CITES附录I种类；附录Ⅱ—列入CITES附录Ⅱ种类；附录Ⅲ—列入CITES附录Ⅲ种类。', 'tableAfterDesc'));
            }
        }
        reportGenerateResult.reportParagraphList = reportParagraphList;
        return reportGenerateResult;
    } catch (error) {
        console.error(error)
        //@ts-ignore
        throw new Error(error.message);

    }
}


export default createSpeciesBook;